<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="订单号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="身份证" prop="idCardNumber">
        <el-input
          v-model="queryParams.idCardNumber"
          placeholder="请输入身份证"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="银行卡" prop="bankCardNo">
        <el-input
          v-model="queryParams.bankCardNo"
          placeholder="请输入银行卡"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号码" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="放款时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="平台" align="center" width="120" prop="companyId"/>
      <el-table-column label="订单号" align="center" width="120" prop="orderSn"/>
      <el-table-column label="贷款用户" align="center" width="180" prop="userName"/>
      <el-table-column label="手机号码" align="center" width="120" prop="userPhone"/>
      <el-table-column label="身份证号码" align="center" width="120" prop="idCardNumber"/>
      <el-table-column label="银行卡" align="center" width="180" prop="bankCardNo"/>
      <el-table-column label="贷款金额" align="center" width="120" prop="realApplyMoney"/>
      <el-table-column label="利息" align="center" width="120" prop="interestMoney"/>
      <el-table-column label="逾期金额" align="center" width="120" prop="overMoney"/>
      <el-table-column label="已还金额" align="center" width="120" prop="alreadyRepayMoney"/>
      <el-table-column label="状态" align="center" width="120" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == '6'">正常</span>
          <span v-if="scope.row.status == '8'" style="color: red">已逾期</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打款时间" align="center" prop="applyTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应还时间" align="center" prop="repaymentTime" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.repaymentTime.substring(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >
            <router-link :to="'/app/user-info/index/' + scope.row.id + '/' + scope.row.userId" class="link-type">
              <span>查看</span>
            </router-link>
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handlePayment(scope.row)"
          >还款
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 分配 -->
    <el-dialog title="还款" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="贷款额度">
          <el-input v-model="form.realApplyMoney" disabled/>
        </el-form-item>

        <el-form-item label="周期">
          <el-input v-model="form.applyCycle" disabled/>
        </el-form-item>

        <el-form-item label="账号管理费">
          <el-input v-model="form.manageMoney" disabled/>
        </el-form-item>

        <el-form-item label="服务费">
          <el-input v-model="form.applyServiceCharge" disabled/>
        </el-form-item>

        <el-form-item label="审核费用">
          <el-input v-model="form.riskMoney" disabled/>
        </el-form-item>

        <el-form-item label="实际打款金额">
          <el-input v-model="form.realPayMoney" disabled/>
        </el-form-item>

        <el-form-item label="利息">
          <el-input v-model="form.interestMoney" disabled/>
        </el-form-item>

        <el-form-item label="逾期天数">
          <el-input v-model="form.overDay" disabled/>
        </el-form-item>

        <el-form-item label="逾期费用">
          <el-input v-model="form.overMoney" disabled/>
        </el-form-item>

        <el-form-item label="已还金额">
          <el-input v-model="form.alreadyRepayMoney" disabled/>
        </el-form-item>

        <el-form-item label="未还金额">
          <el-input v-model="form.noRepayMoney" disabled/>
        </el-form-item>

        <el-form-item label="还款金额" prop="paymentMoney">
          <el-input v-model="form.paymentMoney" placeholder="请输入还款金额" oninput="value = value.replace(/[^\d]/g,'')"/>
        </el-form-item>


        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :rows="3" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHandlerPayment('0')">未结清</el-button>
        <el-button type="primary" @click="submitHandlerPayment('1')">结 清</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listWaitPaymentApply, handlePayment} from "@/api/app/apply";
import {list} from "@/api/company";

export default {
  name: "WaitPayment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      applyList: null,
      // 公司数据
      companyList: null,
      companyId: this.$store.state.user.companyId,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: undefined,
        userPhone: undefined,
        orderSn: undefined,
        userName: undefined,
        bankCardNo: undefined,
        idCardNumber: undefined,
        // status: '6',
      },
      // 表单校验
      rules: {
        paymentMoney: [
          {required: true, message: "还款金额不能为空", trigger: "blur"},
        ]
      }
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      if (this.companyId) {
        this.queryParams.companyId = this.companyId
      }
      listWaitPaymentApply(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        realApplyMoney: 0,
        manageMoney: 0,
        riskMoney: 0,
        applyServiceCharge: 0,
        applyCycle: 0,
        realPayMoney: 0,
        interestMoney: 0,
        overDay: 0,
        overMoney: 0,
        paymentMoney: 0,
        alreadyRepayMoney: 0,
        noRepayMoney: 0,
      };
      this.resetForm("form");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    handlePayment(row) {
      this.reset();
      this.form = row;
      this.form.remark = '';
      this.form.realPayMoney = this.form.realApplyMoney - this.form.manageMoney - this.form.riskMoney - this.form.applyServiceCharge;
      this.form.noRepayMoney = this.form.realApplyMoney + this.form.overMoney - this.form.alreadyRepayMoney;
      this.open = true;
    },

    submitHandlerPayment(type) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let that = this.form;
          this.$modal.confirm('是否确认对编号为"' + this.form.orderSn + '"的订单还款？').then(function () {
            return handlePayment(that.id, '0', that.paymentMoney, type, type);
          }).then(() => {
            this.getList();
            this.open = false;
            this.$modal.msgSuccess("还款成功");
          }).catch((e) => {
          });
        }
      })
    },


    /** 提交按钮 */
    submitForm: function () {
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != undefined) {
      //       updateIndexApply(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addIndexApply(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id || this.ids;
      // this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
      //   return delIndexApply(ids);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("删除成功");
      // }).catch(() => {
      // });
    },

    // handleMoney(){
    //   this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
    // }
  }
};
</script>
